package com.example.testcontract.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.testcontract.R;
import com.example.testcontract.adapter.CallRecordAdapter;
import com.example.testcontract.bean.Dial;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2014/9/23.
 */
public class CallRecord extends Activity {

    private ListView lv_callRecord;
    private CallRecordAdapter adapter;
    private List<Dial> callRecordList = new ArrayList<Dial>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.call_records);

        lv_callRecord = (ListView) findViewById(R.id.lv_call_record);
        setupListView();
    }

    private void setupListView() {
        adapter = new CallRecordAdapter(this, R.layout.callrecord_item, callRecordList);
        Bundle bundle = getIntent().getExtras();

        Uri uri = CallLog.Calls.CONTENT_URI;
        Cursor cursor = getContentResolver().query(uri, new String[]{
                CallLog.Calls.CACHED_NAME,                 //通话人姓名
                CallLog.Calls.NUMBER,                        //通话人电话号码
                CallLog.Calls.TYPE,                         // 通话类型
                CallLog.Calls.DATE,                         // 通话时间
                CallLog.Calls.DURATION                     // 通话时长
        }, "number = ?", new String[]{ bundle.getString("phone") }, CallLog.Calls.DEFAULT_SORT_ORDER);
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(0);
                String phoneNum = cursor.getString(1);
                int type = cursor.getInt(2);
                String str_type = "";
                if (type == CallLog.Calls.INCOMING_TYPE) {
                    str_type = "呼入";
                } else if (type == CallLog.Calls.OUTGOING_TYPE) {
                    str_type = "呼出";
                } else if (type == CallLog.Calls.MISSED_TYPE) {
                    str_type = "未接";
                }
                Date date = new Date(Long.parseLong(cursor.getString(3)));
                long duration = cursor.getLong(4);

                Dial dialRecord = new Dial();
                dialRecord.setName(name);
                dialRecord.setPhoneNumber(phoneNum);
                dialRecord.setState(str_type);
                dialRecord.setDate(date);
                dialRecord.setDuration(duration);
                callRecordList.add(dialRecord);
            }while (cursor.moveToNext());
        }
        startManagingCursor(cursor);

        lv_callRecord.setAdapter(adapter);
        lv_callRecord.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String number = callRecordList.get(position).getPhoneNumber();
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + number));
                startActivity(intent);
            }
        });
    }
}
